<?php
namespace app\admin\controller;

use app\admin\controller\AdminBaseController;
use app\admin\model\Sys_helps;
use think\Cache;
use think\Db;

/**
 * 系统配置
 * Class System
 */
class SystemController extends BaseController {

    /**
     * 站点配置
     */
    public function index() {
		if ($this->request->isPost()) {
            $site_config   = $this->request->post('site_config/a');
            $data['value'] = serialize($site_config);
            if (Db::name('system')->where('name', 'site_config')->update($data) !== false) {
                Cache::set('site_config', $site_config);
                $this->success('提交成功');
            } else {
                $this->error('提交失败');
            }
        }else{
        	if (Cache::has('site_config')) {
	            $site_config = Cache::get('site_config');
	        } else {
	            $site_config = Db::name('system')->field('value')->where('name', 'site_config')->find();
	            $site_config = unserialize($site_config['value']);
	        }
        	return $this->fetch('index', ['site_config' => $site_config]);
        }
    }
	
	//会员设置
	public function config(){
		if ($this->request->isPost()) {
            $site_config   = $this->request->post('hy_config/a');
            $data['value'] = serialize($site_config);
            if (Db::name('system')->where('name', 'hy_config')->update($data) !== false) {
                Cache::set('hy_config', $site_config);
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }else{
        	if (Cache::has('hy_config')) {
	            $site_config = Cache::get('hy_config');
	        } else {
	            $site_config = Db::name('system')->field('value')->where('name', 'hy_config')->find();
	            $site_config = unserialize($site_config['value']);
	        }
        	return $this->fetch('config', ['hy_config' => $site_config]);
        }
		return $this->fetch();
	}
	
	//合伙人设置
	public function ptconfig(){
		if ($this->request->isPost()) {
            $site_config   = $this->request->post('pt_config/a');
            $data['value'] = serialize($site_config);
            if (Db::name('system')->where('name', 'pt_config')->update($data) !== false) {
                Cache::set('pt_config', $site_config);
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }else{
        	if (Cache::has('site_config')) {
	            $site_config = Cache::get('pt_config');
	        } else {
	            $site_config = Db::name('system')->field('value')->where('name', 'pt_config')->find();
	            $site_config = unserialize($site_config['value']);
	        }
        	return $this->fetch('ptconfig', ['pt_config' => $site_config]);
        }
		return $this->fetch();
	}
	
	//服务协议
	public function service(){
		if ($this->request->isPost()) {
            $site_config =  input("content");
            $data['value'] = serialize($site_config);
            if (Db::name('system')->where('name', 'service_config')->update($data) !== false) {
                Cache::set('service_config', $site_config);
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }else{
        	if (Cache::has('service_config')) {
	            $site_config = Cache::get('service_config');
	        } else {
	            $site_config = Db::name('system')->field('value')->where('name', 'service_config')->find();
	            $site_config = unserialize($site_config['value']);
	        }
        	return $this->fetch('service', ['service_config' => $site_config]);
        }
		return $this->fetch();
	}

	/**
     * 帮助中心
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function help($keyword = '', $ziduan = 'id', $param='add_time', $sort='desc') {
        $map = [];
		$order = $param. ' '.$sort;
		$this->assign('param', $param);
		$this->assign('sort', $sort);
        if ($keyword) {
            $map[$ziduan] = $keyword;
        }
		$this->assign('ziduan', $ziduan);
        $help_list = db('sys_helps')->where($map)->order($order)->paginate(15, false, get_query());
        return $this->fetch('help', ['help_list' => $help_list, 'keyword' => $keyword]);
    }

    /**
     * 添加帮助
     * @return mixed
     */
    public function add() {
        return $this->fetch();
    }

    /**
     * 保存帮助
     */
    public function save() {
        if ($this->request->isPost()) {
            $data = $this->request->post();
			$data['add_time'] = now_time();
			$help_model = new Sys_helps();
            if ($help_model->allowField(true)->save($data)) {
                $this->success('添加成功',url('help'));
            } else {
                $this->error('添加失败');
            }
        }
    }

    /**
     * 编辑帮助
     * @param $id
     * @return mixed
     */
    public function edit($id) {
    	$help_model = new Sys_helps();
        $help = $help_model->find($id);
        return $this->fetch('edit', ['help' => $help]);
    }

    /**
     * 更新帮助
     * @param $id
     */
    public function update($id) {
        if ($this->request->isPost()) {
            $data = $this->request->post();
			$help_model = new Sys_helps();
            if ($help_model->allowField(true)->save($data,['id'=>$id]) !== false) {
                $this->success('更新成功',url('help'));
            } else {
                $this->error('更新失败');
            }
        }
    }
	
	//删除帮助
	public function delete_item(){
		$res = del_data("sys_helps",input("id"));
		if($res){
			 $this->success('删除成功',url("help"));
		}else{
			 $this->success('删除失败');
		}
	}
	
	//充值协议
	public function recharge(){
		if ($this->request->isPost()) {
            $site_config =  input("content");
            $data['value'] = serialize($site_config);
            if (Db::name('system')->where('name', 'recharge_config')->update($data) !== false) {
                Cache::set('recharge_config', $site_config);
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }else{
        	if (Cache::has('recharge_config')) {
	            $site_config = Cache::get('recharge_config');
	        } else {
	            $site_config = Db::name('system')->field('value')->where('name', 'recharge_config')->find();
	            $site_config = unserialize($site_config['value']);
	        }
        	return $this->fetch('recharge', ['recharge_config' => $site_config]);
        }
		return $this->fetch();
	}
}
